package com.shisfish.library.Utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.reader.ReaderBuilder;
import net.sf.jxls.reader.XLSReader;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.xml.sax.SAXException;


/**
 * 公用的处理方法, 将导入的excel文件转为 java bean list
 * @author Administrator
 * @param <T>
 */

public class ExcelToBean<T> {
	
	public List<T> parseExcelFileToBeans(File xlsFile, File jxlsConfigFile) throws Exception {
		List<T> result = new ArrayList<T>();
		
		Map<String, Object> beans = new HashMap<String, Object>();
        beans.put("result", result);
        
        InputStream inputStream = null;
        
        try {
        	
        	XLSReader xlsReader = ReaderBuilder.buildFromXML(jxlsConfigFile);
            inputStream = new BufferedInputStream(new FileInputStream(xlsFile));
            xlsReader.read(inputStream, beans);            
        
        } catch (IOException e) {
            
        	throw e;
            
        } catch (SAXException e) {
            
        	throw e;
            
        } catch (InvalidFormatException e) {
            
        	throw e;
            
        }finally{
            
        	if (inputStream !=null){
            
        		inputStream.close();
            
        	}
        }        
        
        return result;
	}
	
}

